/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Visao.Funcionario.Pedido;

import Controle.Controlador;
import Visao.TelaPrincipal;
import javax.swing.JOptionPane;

/**
 *
 * @author Eder
 */
public class ReceberPagamento extends javax.swing.JFrame {
    int codigoPedido;
    TelaPrincipal tela;
    /**
     * Creates new form ReceberPagamento
     */
    public ReceberPagamento(TelaPrincipal tela,int codigoPedido, Float valor) {
        initComponents();
        this.tela = tela;
        this.codigoPedido = codigoPedido;
        jtTotal.setText(setFormatoMoeda(""+valor));
        jtDinheiro.setText("R$  0.00");
        jtTroco.setText(setFormatoMoeda("0"));
    }
    
    public boolean validarPagamento(){
        try {
            Float total = Float.parseFloat(jtTotal.getText().replace("R$ ", ""));
            Float dinheiro =Float.parseFloat(jtDinheiro.getText().replace("R$ ", ""));
            Float troco = (dinheiro - total);
            if(troco<0){
                throw new Exception("Valor que está Sendo Pago é menor que o Valor do Pedido!");
            }
            if(!jrbDinheiro.isSelected() && !jrbCheque.isSelected() && !jrbCartaoDebito.isSelected() && !jrbCartaoCredito.isSelected() ){
                throw new Exception("Selecione uma Forma de Pagamento!");
            }
            return  true;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, "Erro ao Receber Pagamento!\n"+e.getMessage(), "Receber Pagamento", 0);
            return false;
        }
    }
    
    private String setFormatoMoeda(String str){
        str=str.replace("R$ ", "");
        str=str.replace(" ", "");
        
        if(str.contains(".")){
            int index = str.indexOf(".");
            String parteInteira = str.substring(0, index);
            String parteFracionaria = str.substring(index+1);
            if(parteFracionaria.length()<2)
                parteFracionaria+="0";
            if(parteFracionaria.length()>2)
                parteFracionaria = parteFracionaria.substring(0, 1);
            //jtaxaEntregaTF.setText("R$ "+parteInteira+","+parteFracionaria);
            return "R$ "+parteInteira+"."+parteFracionaria;
        }
        else{;
            return "R$ "+str+".00";
        }
        
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jrbCartaoCredito = new javax.swing.JRadioButton();
        jrbCartaoDebito = new javax.swing.JRadioButton();
        jrbCheque = new javax.swing.JRadioButton();
        jrbDinheiro = new javax.swing.JRadioButton();
        jtTroco = new javax.swing.JTextField();
        jtTotal = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jbCancelar = new javax.swing.JButton();
        jbConcluir = new javax.swing.JButton();
        jtDinheiro = new javax.swing.JFormattedTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Receber Pagamento");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("Dinheiro:");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("TOTAL:");

        buttonGroup1.add(jrbCartaoCredito);
        jrbCartaoCredito.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jrbCartaoCredito.setText("Cartão Crédito");
        jrbCartaoCredito.setPreferredSize(new java.awt.Dimension(115, 20));

        buttonGroup1.add(jrbCartaoDebito);
        jrbCartaoDebito.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jrbCartaoDebito.setText("Cartão Débito");
        jrbCartaoDebito.setPreferredSize(new java.awt.Dimension(115, 20));

        buttonGroup1.add(jrbCheque);
        jrbCheque.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jrbCheque.setText("Cheque");
        jrbCheque.setPreferredSize(new java.awt.Dimension(115, 20));

        buttonGroup1.add(jrbDinheiro);
        jrbDinheiro.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jrbDinheiro.setText("Dinheiro");
        jrbDinheiro.setPreferredSize(new java.awt.Dimension(115, 20));

        jtTroco.setEditable(false);
        jtTroco.setPreferredSize(new java.awt.Dimension(71, 20));

        jtTotal.setEditable(false);
        jtTotal.setPreferredSize(new java.awt.Dimension(71, 20));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Troco:");

        jbCancelar.setText("Cancelar");
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });

        jbConcluir.setText("OK");
        jbConcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbConcluirActionPerformed(evt);
            }
        });

        try {
            jtDinheiro.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("R$ **.**")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jtDinheiro.setToolTipText("");
        jtDinheiro.setPreferredSize(new java.awt.Dimension(71, 20));
        jtDinheiro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtDinheiroActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jrbDinheiro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jrbCheque, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jrbCartaoDebito, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jrbCartaoCredito, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(89, 89, 89)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(2, 2, 2)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jtTotal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jtTroco, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jtDinheiro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 2, Short.MAX_VALUE))
                    .addComponent(jSeparator1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jbCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbConcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jrbDinheiro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jtTotal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jrbCheque, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jrbCartaoDebito, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jtDinheiro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jrbCartaoCredito, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jtTroco, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 5, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbCancelar)
                    .addComponent(jbConcluir))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    public Float getDinheiro(){
        Float dinheiro ;
        try {
            String aux = jtDinheiro.getText().replace("R$ ", "");
            aux = aux.replace(" ", "");
            dinheiro =Float.parseFloat(aux);
            return dinheiro;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, "O valor do Campo Dinheiro é Inválido!\n"+e.getMessage(), "Receber Pagamento", 0);
            return null;
        }
    }
    
    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed
        // Botão Cancelar
        this.setVisible(false);
    }//GEN-LAST:event_jbCancelarActionPerformed

    private void jbConcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbConcluirActionPerformed
        // Botão Conclir
        try {
            if(validarPagamento()){
                Controlador.pagar_Pedido(codigoPedido);
                this.tela.atualizaTabela();
                this.setVisible(false);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, "Erro ao Receber Pagamento!\n"+e.getMessage(), "Receber Pagamento", 0);
        }
    }//GEN-LAST:event_jbConcluirActionPerformed

    private void jtDinheiroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtDinheiroActionPerformed
        // Ao clicar no Dinheiro
        try {
            Float total = Float.parseFloat(jtTotal.getText().replace("R$ ", ""));
            Float dinheiro = getDinheiro() ;
            
            if(dinheiro != null){
                String troco = ""+(dinheiro - total);
                jtTroco.setText(setFormatoMoeda(troco));
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, "Erro ao Calcular o troco!\n"+e.getMessage(), "Receber Pagamento", 0);
        }
    }//GEN-LAST:event_jtDinheiroActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JButton jbConcluir;
    private javax.swing.JRadioButton jrbCartaoCredito;
    private javax.swing.JRadioButton jrbCartaoDebito;
    private javax.swing.JRadioButton jrbCheque;
    private javax.swing.JRadioButton jrbDinheiro;
    private javax.swing.JFormattedTextField jtDinheiro;
    private javax.swing.JTextField jtTotal;
    private javax.swing.JTextField jtTroco;
    // End of variables declaration//GEN-END:variables
}
